/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.config;

import java.io.InputStream;
import java.util.Properties;

import org.telosys.util.ClassLoaderUtil;

/**
 * The ClassPath implementation of ConfigLoader
 * 
 * @author Laurent Guerin
 * @since 1.2.0
 *
 */
public class ClassPathConfigLoader extends GenericConfigLoader implements ConfigLoader
{
	private String propertiesFileName = null ;
	
    //------------------------------------------------------------------------------------------------
	/* (non-Javadoc)
	 * @see org.objectweb.telosys.common.config.ConfigLoader#getOrigin()
	 */
	public String getOrigin() {
		return "Class Path" ;
	}

    //------------------------------------------------------------------------------------------------
	/**
	 * Constructor to load a specific file name
	 * @param propertiesFileName
	 */
	public ClassPathConfigLoader(String propertiesFileName) {
		super();
		this.propertiesFileName = propertiesFileName ;
	}
	
    //------------------------------------------------------------------------------------------------
	/* (non-Javadoc)
	 * @see org.objectweb.telosys.common.config.ConfigLoader#loadTelosysProperties()
	 */
	public TelosysConfig loadTelosysProperties() throws Exception
	{
		if ( propertiesFileName != null ) {
			return loadPropertiesFromClassPath ( propertiesFileName );
		}
		else {
			// return loadPropertiesFromClassPath ( ConfigFileNames.TELOSYS_PROPERTIES );
			throw new Exception("Cannot get resource by ClassPath : resource name is null" );
		}
	}
	
    //------------------------------------------------------------------------------------------------
	/* (non-Javadoc)
	 * @see org.objectweb.telosys.common.config.ConfigLoader#getInputStream(java.lang.String)
	 */
	public InputStream getInputStream ( String sResource ) throws Exception
    {
//        ClassLoader cl = ClassLoaderUtil.getClassLoader() ;
//        if ( cl != null )
//        {
//        	InputStream is = cl.getResourceAsStream(sResource);
//        	if ( null == is ) {
//            	throw new Exception("Cannot get resource '" + sResource + "' by ClassPath (not found)" );
//        	}
//        	return is ;
//        }
//        else
//        {
//        	throw new Exception("Cannot get ClassLoader !" );
//        }
        return ClassLoaderUtil.getResourceAsStream(sResource);
    }
	
    //------------------------------------------------------------------------------------------------
    /**
     * Loads properties via the ClassLoader (using the ClassPath )
     * The properties file can be located in a folder or a JAR file accessible via the CLASSPATH
     * ( the WebApp WAR file can be exploded or not )
     * @param sResource
     * @return configuration instance
     */
    private TelosysConfig loadPropertiesFromClassPath ( String sResource ) throws Exception
    {
        TelosysConfig tp = null ;
        
        InputStream is = getInputStream ( sResource ) ;
        if ( is != null )
        {
            Properties prop = loadPropertiesInputStream(is);
            //tp = buildClassPathProperties ( prop, sResource, cl );   
            if ( prop != null ) {
                tp = new TelosysConfig(prop, sResource, this );
            }
        }
        else
        {
            //log.trace("loadPropertiesFromClassPath() : resource '" + sResource + "' not found " );
            throw new Exception("Cannot load properties by ClassPath : resource '" + sResource + "' not found ");
        }
        
        return tp ;
    }

}
